package cn.db101.fairy.service

import cn.db101.fairy.entity.ChineseCharacter
import cn.db101.fairy.mapper.HzMapper
import com.mybatisflex.core.query.QueryMethods.rand
import com.mybatisflex.kotlin.extensions.db.insert
import com.mybatisflex.kotlin.extensions.db.queryOne
import com.mybatisflex.kotlin.extensions.kproperty.eq
import com.mybatisflex.kotlin.extensions.wrapper.whereWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

/**
 * @author zooy
 * @since 2024/5/20-2:18
 */
@Service
class HzService {

    @Autowired
    lateinit var hzMapper: HzMapper

    fun one(name: String): ChineseCharacter? {

        return queryOne {
            whereWith {
                ChineseCharacter::name eq name
            }
        }
    }

    fun random(): ChineseCharacter {

        return hzMapper.randomOne()
    }

    fun update(chineseCharacter: ChineseCharacter) {

        val character = one(chineseCharacter.name!!)

        insert(character?.also {
            it.pinyin = chineseCharacter.pinyin
            it.voiceText = chineseCharacter.voiceText
            it.bushou = chineseCharacter.bushou
            it.bihua = chineseCharacter.bihua
            it.struct = chineseCharacter.struct
            it.textDesc = chineseCharacter.textDesc
            it.baseText = chineseCharacter.baseText
            it.detailText = chineseCharacter.detailText

        }!!)

    }

}
